package com.woniu.p2p.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.p2p.entity.User;
import com.woniu.p2p.entity.UserBank;
import com.woniu.p2p.mapper.UserBankMapper;
import com.woniu.p2p.mapper.UserMapper;
import com.woniu.p2p.service.MoneyManageService;
import com.woniu.p2p.vo.DrawVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 86152
 * @create 2020/8/9
 * @since 1.0.0
 */
@Service
public class MoneyManageServiceImp implements MoneyManageService {
    @Autowired
    UserMapper userMapper;

    /*获取账户余额*/
    @Override
    public Long findAmountById(int userid) {
        return userMapper.findAmountById(userid);
    }

    /*通过userid查询支付密码*/
    @Override
    public String findPaypwdByUserid(int userid) {
        return userMapper.findPaypwdByUserid(userid);
    }

    /*更新账户余额*/
    @Override
    public int drawnow(Long drawmoney, int userid) {
        /*通过userid查询用户获取余额*/
        User user = userMapper.selectByPrimaryKey(userid);
        /*余额不足*/
        if(user.getUserAmount()<drawmoney){
            return 0;
            /*余额足够*/
        }else{
            int row=userMapper.releaseAmount(drawmoney,userid);
            return row;
        }
    }

    @Autowired
    UserBankMapper userBankMapper;

    /*获取银行卡下拉列表数据*/
    @Override
    public List<UserBank> getbanklist(int userid) {
        return userBankMapper.selectByUserid(userid);
    }

    /*根据userid和银行卡id获取银行名*/
    @Override
    public String findBanknameBycardIdandUserid(String cardId, int userid) {
        return userBankMapper.findBanknameBycardIdandUserid(cardId,userid);
    }

    /*添加提现记录*/
    @Override
    public void addDrawRecoed(Date date, Long drawmoney, String cardId, String bankName, String status, String context, String remark,int userid) {
        userBankMapper.addDrawRecoed(date,drawmoney,cardId,bankName,status,context,remark,userid);
    }

    /*查询提现记录*/
    @Override
    public PageInfo<DrawVo> findalldrawsByuserid(int userid) {
        PageHelper.startPage(1,4);
        List<DrawVo> drawVos=userBankMapper.findalldrawsByuserid(userid);
        PageInfo<DrawVo> drawVoPageInfo = new PageInfo<>(drawVos);
        return drawVoPageInfo;
    }

    /*条件查询提现记录*/
    @Override
    public PageInfo<DrawVo> finddrawbycons(String drawcontext, Date starttime, Date endtime, Integer start, int size, int userid) {
        PageHelper.startPage(start,size);
        List<DrawVo> drawVos=userBankMapper.finddrawbycons(drawcontext,starttime,endtime,userid);
        PageInfo<DrawVo> drawVoPageInfo = new PageInfo<>(drawVos);
        return drawVoPageInfo;
    }

    /*删除提现记录并进行条件查询*/
    @Override
    public PageInfo<DrawVo> deletedrawbyid(Integer drawId, String drawcontext, Date starttime, Date endtime, Integer start, int size, int userid) {
        PageHelper.startPage(start,size);
        /*根据id删除提现记录*/
        userBankMapper.deletedrawbyid(drawId);
        /*条件查询*/
        List<DrawVo> finddrawbycons = userBankMapper.finddrawbycons(drawcontext, starttime, endtime, userid);
        PageInfo<DrawVo> drawVoPageInfo = new PageInfo<>(finddrawbycons);
        return drawVoPageInfo;
    }
}
